package top.drinyeit.nongmao.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

import top.drinyeit.nongmao.R;
import top.drinyeit.nongmao.base.BaseActivity;
import top.drinyeit.nongmao.common.CommonPagerAdapter;
import top.drinyeit.nongmao.fragment.OrderFragment;

/**
 * Created by YJY on 2018/1/12.
 *
 * @param "position" int
 */

public class MyOrderActivity extends BaseActivity {

    ImageView back;
    TabLayout orderTab;
    ViewPager orderPager;
    List<Fragment> orderFragments;
    int position = 4;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myorder);
        back = findViewById(R.id.iv_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        initFragments();
        initTabAndPager();
        initIntent();
        setTitle();
    }

    private void initIntent(){
        Bundle bundle = getIntent().getExtras();
        if (bundle != null){
            position = bundle.getInt("position");
            orderPager.setCurrentItem(position);
        }
    }

    private void initTabAndPager() {
        orderTab = (TabLayout) findViewById(R.id.tab_order);
        orderPager = (ViewPager) findViewById(R.id.vp_order);
        orderPager.setAdapter(new CommonPagerAdapter(MyOrderActivity.this, orderFragments, R.array.OrderTitles, getSupportFragmentManager()));
        orderPager.setOffscreenPageLimit(5);
        orderTab.setupWithViewPager(orderPager);
        orderPager.setCurrentItem(getIntent().getIntExtra("order_position", 0), false);
    }

    private void initFragments() {
        orderFragments = new ArrayList<>();
        orderFragments.add(new OrderFragment(0));
        orderFragments.add(new OrderFragment(1));
        orderFragments.add(new OrderFragment(2));
        orderFragments.add(new OrderFragment(3));
        orderFragments.add(new OrderFragment(-1));
    }
}
